package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.enterprise.MyNeedQuoteListEntity;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MySignUpListViewModel extends AndroidViewModel {
    public MutableLiveData<Integer> showLoading = new MutableLiveData<>();
    public MutableLiveData<String> toastUntil = new MutableLiveData<>();
    public MutableLiveData<List<MyNeedQuoteListEntity>> listOffer = new MutableLiveData<>();

    public int pageSize = 1;

    public MySignUpListViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取我的报价列表
     *
     * @param map
     */
    public void getDemandOfferList(Map<String, Object> map, Boolean isRefresh) {
        showLoading.postValue(1);
        pageSize = isRefresh ? 1 : pageSize + 1;
        map.put("pageSize", pageSize);
        map.put("limit", 20);
        Log.e("message",new Gson().toJson(map));
        APIManager.getInstance().getNeedAPI().getDemandOfferList(map).enqueue(new Callback<BaseEntity<List<MyNeedQuoteListEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MyNeedQuoteListEntity>>> call, Response<BaseEntity<List<MyNeedQuoteListEntity>>> response) {
                showLoading.postValue(2);
                if (response != null && response.body() != null && response.body().getData() != null && response.body().getData() != null) {
                    listOffer.postValue(response.body().getData());
                } else {
                    listOffer.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MyNeedQuoteListEntity>>> call, Throwable t) {
                showLoading.postValue(2);
            }
        });
    }

}
